/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmTipoConceptos.java
 *
 * Created on 18/10/2012, 01:18:12 AM
 */

package co.edu.unitecnologica.interfaz;

import co.edu.unitecnologica.modelo.Conceptos;
import co.edu.unitecnologica.modelo.ConceptosEstudiantes;
import co.edu.unitecnologica.modelo.ConceptosEstudiantesPK;
import co.edu.unitecnologica.modelo.Estudiantes;
import co.edu.unitecnologica.modelo.Programas;
import java.util.Calendar;
import java.util.List;
import javax.persistence.Query;

/**
 *
 * @author osalcedo
 */
public class FrmConceptosEstudiantes extends Formulario {

    Estudiantes estudiante;

    /** Creates new form FrmTipoConceptos */
    public FrmConceptosEstudiantes(Principal p, Estudiantes e) {
        super(p);
        estudiante=e;
        initComponents();
        init();
        habilitarCampos(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jmiImprimir = new javax.swing.JMenuItem();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jtfPeriodo = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jcbConceptos = new javax.swing.JComboBox();
        jtfValor = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jcbEstado = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jtfNombreApellidos = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jtfIdEstudiante = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jtfPrograma = new javax.swing.JTextField();

        jmiImprimir.setText("Imprimir");

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Conceptos aplicados al estudiante");

        jLabel5.setText("Concepto:");

        jLabel6.setText("Valor:");

        jcbEstado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Activo", "Inactivo" }));
        jcbEstado.setEditable(false);

        jLabel7.setText("Estado:");

        jLabel4.setText("Periodo:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(3, 3, 3))
                    .addComponent(jLabel7)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jcbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfValor, javax.swing.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)
                    .addComponent(jtfPeriodo, javax.swing.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)
                    .addComponent(jcbConceptos, javax.swing.GroupLayout.Alignment.TRAILING, 0, 269, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfPeriodo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbConceptos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        jLabel1.setText("ID:");

        jtfNombreApellidos.setEditable(false);

        jLabel2.setText("Apellidos y Nombres:");

        jtfIdEstudiante.setEditable(false);

        jLabel3.setText("Programa:");

        jtfPrograma.setEditable(false);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jtfPrograma)
                    .addComponent(jtfNombreApellidos)
                    .addComponent(jtfIdEstudiante, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfIdEstudiante, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfNombreApellidos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtfPrograma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JComboBox jcbConceptos;
    private javax.swing.JComboBox jcbEstado;
    private javax.swing.JMenuItem jmiImprimir;
    private javax.swing.JTextField jtfIdEstudiante;
    private javax.swing.JTextField jtfNombreApellidos;
    private javax.swing.JTextField jtfPeriodo;
    private javax.swing.JTextField jtfPrograma;
    private javax.swing.JTextField jtfValor;
    // End of variables declaration//GEN-END:variables

    @Override
    public boolean insertarRegistro() {
        try{
            em.getTransaction().begin();
            ConceptosEstudiantes t= new ConceptosEstudiantes();
            ConceptosEstudiantesPK pk= new ConceptosEstudiantesPK();

            pk.setCodConcepto(Integer.parseInt(jcbConceptos.getSelectedItem().toString().split("-")[0]));
            pk.setCodPeriodo(Integer.parseInt(jtfPeriodo.getText()));
            pk.setCodPrograma(estudiante.getEstudiantesPK().getCodPrograma());
            pk.setIdEstudiante(estudiante.getEstudiantesPK().getId());
            t.setConceptosEstudiantesPK(pk);

            t.setValor(Double.parseDouble(jtfValor.getText()));
            t.setEstado(this.jcbEstado.getSelectedItem().toString());

            t.setFechaActividad(Calendar.getInstance().getTime());
            t.setHoraActividad(Calendar.getInstance().getTime());
            t.setUsuario("usuario");
            em.persist(t);
            em.getTransaction().commit();            
            return true;
        }catch(Exception e){
            em.getTransaction().rollback();
            return false;
        }
    }

    @Override
    public boolean buscarRegistros() {
        try{
            String sql="SELECT t.* "
                    + " FROM conceptos_estudiantes t"
                    + " WHERE "
                    + "     t.id_estudiante like '%"+estudiante.getEstudiantesPK().getId()+"%' "
                    + "     and t.cod_programa like '%"+estudiante.getEstudiantesPK().getCodPrograma()+"%' "
                    + "     and t.cod_periodo like '%"+jtfPeriodo.getText()+"%' "
                    + "     and t.cod_concepto like '%"+jcbConceptos.getSelectedItem().toString().split("-")[0]+"%' ";
            query = em.createNativeQuery(sql,ConceptosEstudiantes.class);
            lista= query.getResultList();
            this.setRegistroActual(0);
            this.setRegistrosTotales(lista.size());
            this.mostrarRegistro(0);
            return true;
        }catch(Exception e){
            return false;
        }
    }

    @Override
    public boolean editarRegistro() {
        try{
            em.getTransaction().begin();
            ConceptosEstudiantes t= (ConceptosEstudiantes) lista.get(getRegistroActual());
            ConceptosEstudiantesPK pk= new ConceptosEstudiantesPK();

            pk.setCodConcepto(Integer.parseInt(jcbConceptos.getSelectedItem().toString().split("-")[0]));
            pk.setCodPeriodo(Integer.parseInt(jtfPeriodo.getText()));
            pk.setCodPrograma(estudiante.getEstudiantesPK().getCodPrograma());
            pk.setIdEstudiante(estudiante.getEstudiantesPK().getId());
            t.setConceptosEstudiantesPK(pk);

            t.setValor(Double.parseDouble(jtfValor.getText()));
            t.setEstado(jcbEstado.getSelectedItem().toString());

            t.setFechaActividad(Calendar.getInstance().getTime());
            t.setHoraActividad(Calendar.getInstance().getTime());
            t.setUsuario("usuario");
            em.getTransaction().commit();            
            return true;
        }catch(Exception e){
            em.getTransaction().rollback();
            return false;
        }
    }

    @Override
    public boolean borrarRegistro() {
        try{
            em.getTransaction().begin();
            ConceptosEstudiantes t= (ConceptosEstudiantes) lista.get(getRegistroActual());
            em.remove(t);
            em.getTransaction().commit();            
            return true;
        }catch(Exception e){
            em.getTransaction().rollback();
            return false;
        }
    }

    @Override
    public void mostrarRegistro(int r) {
        if(lista.size()>0 && r<lista.size()){
            ConceptosEstudiantes t=(ConceptosEstudiantes) lista.get(r);
            jtfPeriodo.setText(String.valueOf(t.getConceptosEstudiantesPK().getCodPeriodo()));
            jtfValor.setText(String.valueOf(t.getValor()));
            this.jcbEstado.setSelectedItem(t.getEstado());
            Conceptos c=em.getReference(Conceptos.class, t.getConceptosEstudiantesPK().getCodConcepto());
            jcbConceptos.setSelectedItem(c.getCod()+"-"+c.getDescripcion());
        }
    }

    @Override
    public void cargarRegistros() {
        try{
            String sql="SELECT t.* "
                    + " FROM conceptos_estudiantes t"
                    + " WHERE "
                    + "     t.id_estudiante like '%"+estudiante.getEstudiantesPK().getId()+"%' "
                    + "     and t.cod_programa like '%"+estudiante.getEstudiantesPK().getCodPrograma()+"%' ";
            query = em.createNativeQuery(sql,ConceptosEstudiantes.class);
            lista=query.getResultList();
            setRegistroActual(0);
            setRegistrosTotales(lista.size());
            mostrarRegistro(getRegistroActual());
        }catch(Exception e){}
    }

    @Override
    public void limpiarCampos() {
        this.jtfPeriodo.setText("");
        this.jtfValor.setText("");
    }

    @Override
    public final void habilitarCampos(boolean v) {
        this.jtfPeriodo.setEditable(v);
        this.jtfValor.setEditable(v);
        this.jcbConceptos.setEditable(v);
        this.jcbEstado.setEditable(v);
    }

    @Override
    public void agregarItemsMenuOpciones() {
    }

    private void init() {
        Query q = em.createNamedQuery("Conceptos.findAll");
        List l = q.getResultList();
        for(int i=0;i<l.size();i++){
            Conceptos c=(Conceptos) l.get(i);
            jcbConceptos.addItem(c.getCod()+"-"+c.getDescripcion());
        }
        jtfIdEstudiante.setText(estudiante.getEstudiantesPK().getId());
        Programas p=em.getReference(Programas.class, estudiante.getEstudiantesPK().getCodPrograma());
        jtfNombreApellidos.setText(estudiante.getApellidos()+", "+estudiante.getNombres());
        jtfPrograma.setText(p.getDescripcion());
    }

}
